/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Tests;

import dao.UsersFacade;
import entity.Users;
import entity.IdentificationType;
import entity.UserType;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Diego
 */
public class UserFunctions{
    
    public void CreateUser() {
        try {
            Users u = new Users();
            u.setActive(Boolean.TRUE);
            u.setIdentificationNumber(new Long(getRandomNumber()));
            u.setIdentificationType(IdentificationType.NATIONAL_IDENTIFICATION);
            u.setUserType(UserType.TEACHER);
            u.setName("NamebyUser1");
            u.setSurname("SurmanebyUser1");
            u.setUserName("UsernamebyUser1");
            u.setUserPassword("PasswordbyUser1");
            create(u);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public void testEditUser() {
        try {
            Users u_updated = new Users();
            u_updated.setActive(Boolean.TRUE);
            u_updated.setId(new Long("6"));
            u_updated.setIdentificationNumber(new Long(getRandomNumber()));
            u_updated.setIdentificationType(IdentificationType.NATIONAL_IDENTIFICATION);
            u_updated.setName("NamebyUser1");
            u_updated.setSurname("SurmanebyUser1");
            u_updated.setUserName("UsernamebyUser1");
            u_updated.setUserPassword("PasswordbyUser1");
            edit(u_updated);
        } catch (Exception ex) {
            Logger.getLogger(UserFunctions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void testFindById() {
        try {
            Users find = find(new Long("6"));
            System.out.println("Users Name: " + find.getName() + " Surmane: " + find.getSurname());
        } catch (Exception ex) {
            Logger.getLogger(UserFunctions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void testFindAllUsers() {
        try {
            List<Users> AllUsers = findAll();
            Iterator<Users> iterator = AllUsers.iterator();
            while (iterator.hasNext()) {
//            iterator.next();
                Users usr = iterator.next();
                System.out.println("Users Name: " + usr.getName() + " ID: " + usr.getId());
            }
        } catch (Exception ex) {
            Logger.getLogger(UserFunctions.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
//    public static void testDeleteUser() {
//        Users find = find(new Long("103"));
//        remove(find);
//        
//    }
    
    private static String getRandomNumber(){
        Random rnd = new Random();
        int n = 10000000 + rnd.nextInt(90000000);
        return ""+n;
    }

    private UsersFacade usersFacade = new UsersFacade();

    public void create(Users user) throws Exception {
        usersFacade.create(user);
    }

    public void edit(Users user) throws Exception {
        usersFacade.edit(user);
    }

    public void remove(Users user) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); 
    }
    
    public Users find(Object id) throws Exception {
        return usersFacade.find(id);
    }

    public List<Users> findAll() throws Exception {
        return usersFacade.findAll();
    }
    
    
}
